// 初始化插件
export default class WebVideo {
    // g_iWndIndex: number = 0;
    // szDeviceIdentify: string = '';
    // deviceport: string = '';
    // channels: any;
    // ip: string = '192.168.1.1';
    // port: string = '80';
    // username: string = 'admin';
    // password: string = 'G123456';

    g_iWndIndex = 0;
    szDeviceIdentify='';
    deviceport ='';
    channels;
    ip = '192.168.0.221';
    port = '80';
    username = 'admin';
    password = 'abcd1234';
    init() {
      // 检查插件是否已经安装过
      let iRet = WebVideoCtrl.I_CheckPluginInstall();
      console.log(iRet);
      if (-1 == iRet) {
        alert('您还未安装过插件，双击开发包目录里的WebComponentsKit.exe安装！');
        return;
      }
      // 初始化插件参数及插入插件
      WebVideoCtrl.I_InitPlugin('100%', '100%', {
        bWndFull: true,
        iPackageType: 2,
        iWndowType: 1,
        cbInitPluginComplete() {
          WebVideoCtrl.I_InsertOBJECTPlugin('divPlugin');
        }
      });
    }
    // 登录
    clickLogin() {
      if ('' == this.ip || '' == this.port) {
        return;
      }
      console.log(this.ip + '_' + this.port, 'pppppppppp')
      this.szDeviceIdentify = this.ip + '_' + this.port;
      WebVideoCtrl.I_Login(this.ip, 1, this.port, this.username, this.password, {
        success: (xmlDoc) => {
          setTimeout(() => {
            this.getChannelInfo();
            this.getDevicePort();
          }, 10);
          setTimeout(() => {
            this.clickStartRealPlay();
          }, 500);
          console.error('登录成功');
        },
        error: function(status, xmlDoc) {
          console.log('登录失败', status, xmlDoc);
        }
      });
    }
    // 退出
    clickLogout() {
      if (null == this.szDeviceIdentify) {
        return;
      }
      let iRet = WebVideoCtrl.I_Logout(this.szDeviceIdentify);
      if (0 == iRet) {
        this.getChannelInfo();
        this.getDevicePort();
      }
    }
    // 获取通道
    getChannelInfo() {
      this.channels = [];
      if (null == this.szDeviceIdentify) {
        return;
      }
      // 模拟通道
      WebVideoCtrl.I_GetAnalogChannelInfo(this.szDeviceIdentify, {
        async: false,
        success: (xmlDoc) => {
          let oChannels = xmlDoc.getElementsByTagName('VideoInputChannel');
          console.log('获取模拟通道成功', xmlDoc);
          for (let i = 0; i < oChannels.length; i++) {
              let id = oChannels[i].querySelector('id').firstChild.nodeValue;
              let name = oChannels[i].querySelector('name').firstChild.nodeValue;
            if ('' == name) {
              name = 'Camera ' + (i < 9 ? '0' + (i + 1) : i + 1);
            }
            this.channels.push({
              id: id,
              name: name
            });
          }
        },
        error: function(status, xmlDoc) {
          console.log('获取模拟通道失败', status);
        }
      });
    }
    // 获取端口
    getDevicePort() {
      if (null === this.szDeviceIdentify) {
        return;
      }
      let oPort = WebVideoCtrl.I_GetDevicePort(this.szDeviceIdentify);
      if (oPort != null) {
        this.deviceport = oPort.iDevicePort;
        this.deviceport = oPort.iRtspPort;
      }
      console.log('端口:', oPort);
    }
    // 开始预览
    clickStartRealPlay() {
      let oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex),
        iChannelID = +this.channels[0].id;
      if (null == this.szDeviceIdentify) {
        return;
      }
      const startRealPlay = () => {
        WebVideoCtrl.I_StartRealPlay(this.szDeviceIdentify, {
          iRtspPort: this.deviceport,
          iStreamType: 1,
          iChannelID: iChannelID,
          bZeroChannel: false,
          success: (status) => {
            console.log('预览成功', status);
          },
          error: (status, xmlDoc) => {
            console.log('预览失败', status);
            if (403 === status) {
            } else {
            }
          }
        });
      };
      if (oWndInfo != null) {
        // 已经在播放了，先停止
        WebVideoCtrl.I_Stop({
          success: function() {
            startRealPlay();
          }
        });
      } else {
        startRealPlay();
      }
    }
    // 停止预览
    clickStopRealPlay() {
      let oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
      if (oWndInfo != null) {
        WebVideoCtrl.I_Stop({
          success: function() {
            console.log('停止播放成功');
          },
          error: function() {
            console.log('停止播放失败');
          }
        });
      }
    }
  }